/*
 * Copyright (C) 2016-2017 AnStudio
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.astudio.lhasa.workers.translators;

import com.astudio.lhasa.interfaces.IInterpreterCallback;
import com.astudio.lhasa.utils.HttpRequester;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.astudio.lhasa.constants.InterpreterError.INTERPRETER_ERROR_TRANSLATE_COMMON_ERROR;
import static com.astudio.lhasa.constants.InterpreterError.INTERPRETER_ERROR_TRANSLATE_NOT_INITIALIZED;

public class OnlineTranslator extends Translator {
    private static volatile OnlineTranslator mInstance = null;

    private static final String TRANSLATOR_REQUEST_PARAM_NAME_SOURCE_LANG   = "src";
    private static final String TRANSLATOR_REQUEST_PARAM_NAME_DEST_LANG     = "tgt";
    private static final String TRANSLATOR_REQUEST_PARAM_NAME_LANGDIRECTION = "lang";

    private static final String[] LANGUAGES = {"ct", "tc"};

    private ExecutorService mService = null;

    private OnlineTranslator() {
        mService = Executors.newFixedThreadPool(5);
    }

    public static OnlineTranslator getInstance() {
        if (mInstance == null) {
            synchronized (OnlineTranslator.class) {
                mInstance = new OnlineTranslator();
            }
        }

        return mInstance;
    }

    private HashMap formRequestParam(int direction, String input) {
        HashMap params = new LinkedHashMap();
        params.put(TRANSLATOR_REQUEST_PARAM_NAME_SOURCE_LANG, input);
        params.put(TRANSLATOR_REQUEST_PARAM_NAME_DEST_LANG, "");
        params.put(TRANSLATOR_REQUEST_PARAM_NAME_LANGDIRECTION, LANGUAGES[direction]);

        return params;
    }

    @Override
    public String requestTranslateLocked(final int direction, final String input) {
        return HttpRequester.requestTranslateLocked(formRequestParam(direction, input));
    }

    @Override
    public void requestTranslateAsync(final int handler, final int direction, final String input
            , final IInterpreterCallback callback) {
        if (callback == null || mService == null) {
            return;
        }

        if (mService == null) {
            callback.onInterpreterError(handler
                    , INTERPRETER_ERROR_TRANSLATE_NOT_INITIALIZED, null);
        }

        mService.submit(new Runnable() {
            @Override
            public void run() {
                String result = requestTranslateLocked(direction, input);

                if (result == null) {
                    callback.onInterpreterError(handler
                            , INTERPRETER_ERROR_TRANSLATE_COMMON_ERROR, null);
                } else {
                    int[] languages = getLanguagesByDirection(direction);
                    callback.onTranslateDone(handler, result, languages[0], languages[1], input);
                }
            }
        });
    }
}
